<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-6-4
 * Time: 上午10:32
 */

define('QRCODE_PATH',  dirname(__FILE__).'/');
require_once QRCODE_PATH.'lib/qrlib.php';

class LogoQRMake {
    protected $data = '';//二维码内容，可能是网址
    protected $logo = null;//logo
    protected $size = 4;//尺寸，为1－10,默认为4
    protected $path = '/Data/Uploads/';//默认存储路径
    protected $QR = null;
    private $errorCorrectionLevel='L';//纠错级别
    private $margin=2;//外边距

    /**
     * name   : 构造函数
     */
    public function __construct($data, $logo, $path='/Data/Uploads/',$size = 4,$errorCorrectionLevel='H',$margin=0) {
        file_put_contents('77.txt',7777);
        $this->data = $data;
        $this->logo = $logo;
        $this->path = $path;
        $this->size = $size;
        $this->errorCorrectionLevel = $errorCorrectionLevel;
        $this->margin = $margin;
    }

    /**
     * @return string
     * 获取文件名
     */
    private function getFileName(){
        $this->makeDir($this->path);
        $randNumber = mt_rand(00000, 99999).  mt_rand(000, 999);
        $fileName = substr(md5($randNumber), 8, 16) .".png";
        return $fileName;
    }

    /**
     * name   : 获取原二维码，用来生成带logo的二维码
     */
    protected function getpng() {
        file_put_contents('6.txt',666);
        $filename = $this->getFileName();
        \QRcode::png($this->data, $this->path.$filename, $this->errorCorrectionLevel, $this->size, $this->margin);
        return $filename;
    }
    /**
     * name    : 输出带LOGO的二维码图片
     * author  : ycgpp@126.com
     */
    public function images() {
        $fileName = $this->getpng();

        if ($this->logo !== FALSE) {
            $img = new Image(2);
            $orgFile = $this->path.$fileName;
            $img->open($orgFile);
            $fileName = $this->getFileName();
            $img->water($this->logo,5,100)->save($this->path.$fileName);
            unlink($orgFile);


//            $this->QR = imagecreatefrompng($this->path.$fileName);
//            unlink($this->path.$fileName);//删除原来的二维码
//
//            $this->logo = imagecreatefromstring(file_get_contents($this->logo));
//
//            $QR_width = imagesx($this->QR);
//
//            $logo_width = imagesx($this->logo);
//            $logo_height = imagesy($this->logo);
//
//            $logo_qr_width = 52;
//            $from_width = ($QR_width - $logo_qr_width) / 2;
//
//            imagecopyresampled($this->QR, $this->logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_width, $logo_width, $logo_height);
//
//            $fileName = $this->getFileName();
//            imagepng($this->QR,$this->path.$fileName);
//            imagedestroy($this->QR);
        }

        return $fileName;
    }

    // 循环创建目录
    private function makeDir($dir, $mode = 0777) {
        if(function_exists('mk_dir')){
            mk_dir($dir,$mode);
        } else {
            $this->mkDir($dir,$mode);
        }
    }

    private function mkDir($dir,$mode=0777){
        if (is_dir($dir) || @mkdir($dir, $mode)){
            return true;
        }

        if (!mkDir(dirname($dir), $mode))
            return false;
        return @mkdir($dir, $mode);
    }
}